package com.xiaocg.juc.future;

import java.util.concurrent.*;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

public class CompletableFutureDemo {
    public static void main(String[] args) throws Exception{
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(3, 10, 0, TimeUnit.SECONDS, new ArrayBlockingQueue<>(10));

        CompletableFuture.supplyAsync(()->{
            int i = ThreadLocalRandom.current().nextInt(10);
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if (i>1){
                int j = 10/0;
            }
            return i;
        },threadPoolExecutor).whenComplete((v,e)->{
            if (e ==null){
                System.out.println("v:"+v);
            }
        }).exceptionally(e->{
            e.printStackTrace();
            return null;
        });


        System.out.println("主线程:"+Thread.currentThread().getName());

        threadPoolExecutor.shutdown();

    }
}
